package com.brotherly.framework.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

public class UpdateManager
{

    private static final int DOWN_OVER = 2;
    private static final int DOWN_UPDATE = 1;
    private static Handler Handler = new Handler()
    {

        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case DOWN_UPDATE:
                    break;
                case DOWN_OVER:
                    Manager.installApk();
                    break;
            }
        }
    };

    private static UpdateManager Manager;
    private static final String SaveFileName = "/Update.apk";

    private String apkUrl;
    private Thread downLoadThread;

    private Context mContext;

    private Runnable mdownApkRunnable = new Runnable()
    {

        @Override
        public void run()
        {
            try
            {
                URL url = new URL(apkUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();
                int length = conn.getContentLength();
                InputStream is = conn.getInputStream();
                File apkFile = new File(SaveFileName);
                FileOutputStream fos = new FileOutputStream(apkFile);
                int count = 0;
                byte buf[] = new byte[1024];
                do
                {
                    int numread = is.read(buf);
                    if (numread == -1)
                    {
                        Handler.sendEmptyMessage(DOWN_OVER);
                        break;
                    }
                    count += numread;
                    fos.write(buf, 0, numread);
                } while (count < length);
                fos.close();
                is.close();

            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    };

    public UpdateManager(Context context)
    {
        this.mContext = context;
        Manager = this;
    }

    public void downloadApk()
    {
        downLoadThread = new Thread(mdownApkRunnable);
        downLoadThread.start();
    }

    public void installApk()
    {
        File apkfile = new File(this.mContext.getFilesDir() + SaveFileName);
        if (!apkfile.exists())
        {
            return;
        } else
        {

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("file：//" + apkfile.toString()), "application/vnd.android.package-archive");
            mContext.startActivity(i);
        }
    }

    public void setDownloadUrl(String url)
    {
        apkUrl = url;
    }

}
